package com.diabeco.ui.measures;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.diabeco.R;
import com.diabeco.data.measures.FoodIntakeMeasureDaoImpl;
import com.diabeco.data.measures.GlycemiaMeasureDaoImpl;
import com.diabeco.model.measures.FoodIntakeMeasure;

public class ListFoodIntakeMeasuresActivity extends Activity {

	private static final int REQUEST_CODE = 1236;

	private List<FoodIntakeMeasure> measures;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_fook_intake_measures);

		this.setMeasures(FoodIntakeMeasureDaoImpl.getInstance(this).selectAll());

		this.initializeMeasuresList();
	}

	public void addFoodIntakeMeasureAction(View view) {

		Intent intent = new Intent(this, MeasureFoodIntakeActivity.class);
		startActivityForResult(intent, REQUEST_CODE);

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		this.setMeasures(FoodIntakeMeasureDaoImpl.getInstance(this).selectAll());

		this.initializeMeasuresList();

	}

	private void initializeMeasuresList() {

		List<FoodIntakeMeasure> measures = this.getMeasures();

		if (!measures.isEmpty()) {
			ListView measuresList = (ListView) findViewById(R.id.measures_list);

			ArrayAdapter<FoodIntakeMeasure> measuresAdapter = new ArrayAdapter<FoodIntakeMeasure>(this,
					android.R.layout.simple_list_item_1, measures);
			measuresList.setAdapter(measuresAdapter);

			measuresList.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

					ArrayAdapter<FoodIntakeMeasure> arrayAdapter = (ArrayAdapter<FoodIntakeMeasure>) ((ListView) parent)
							.getAdapter();
					FoodIntakeMeasure measure = arrayAdapter.getItem(position);

					Intent intent = new Intent(parent.getContext(), MeasureFoodIntakeActivity.class);
					intent.putExtra("measureId", measure.getId());
					startActivity(intent);
				}
			});

			measuresList.setOnItemLongClickListener(new OnItemLongClickListener() {

				public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

					ArrayAdapter<FoodIntakeMeasure> arrayAdapter = (ArrayAdapter<FoodIntakeMeasure>) ((ListView) parent)
							.getAdapter();
					FoodIntakeMeasure foodIntakeMeasure = arrayAdapter.getItem(position);

					FoodIntakeMeasureDaoImpl.getInstance(ListFoodIntakeMeasuresActivity.this).delete(
							foodIntakeMeasure.getId());
					getMeasures().remove(foodIntakeMeasure);

					arrayAdapter.notifyDataSetChanged();

					return true;
				}
			});
		} else {
			// TODO inform the user there are no measures taken
		}
	}

	public List<FoodIntakeMeasure> getMeasures() {
		return measures;
	}

	public void setMeasures(List<FoodIntakeMeasure> measures) {
		this.measures = measures;
	}

}
